﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CARMSData.CollectionClasses;
using CARMSData.EntityClasses;
using CARMSData.HelperClasses;

namespace CARMSBusiness
{
    public class GFlowBusiness
    {
        /// <summary>
        /// 如果g132的是 开始的流程 ,那么就创建 reprot flow entity
        /// </summary>
        /// <param name="reportHeader"></param>
        public static void GenerateReportFlowEntity(ReportHeaderEntity reportHeader)
        {
            var g132Collection = new G132Collection();
            g132Collection.GetMulti(G132Fields.IsStartFlow == true);
            if (g132Collection.Count > 0)
            {
                var reportFlow = new ReportFlowEntity
                {
                    ReportFlowId = Guid.NewGuid(),
                    FlowName = g132Collection[0].G1321,
                    OrginReportNumber = reportHeader.ReportCode,
                    FlowStepId = g132Collection[0].Gid,
                    IsActive = true

                };
                reportFlow.Save();
            }
        }

        /// <summary>
        /// 查询 g130(report  category)中的进入任务,查看g102中的任务列表,并产生消息到 G100,或者直接执行
        /// </summary>
        /// <param name="reportHeaderEntity"></param>
        public static void GenerateReportInFlowTask(ReportHeaderEntity reportHeaderEntity, string strOperatorName)
        {
            var actionList = reportHeaderEntity.ReportCategory.G1308.SeperateString('|');  //g130-8 进入表单事件
            foreach (var strActionName in actionList)
            {
                var g102Entity = ActionBusiness.GetG102EntityByActionName(strActionName);
                if (g102Entity != null)
                {
                    var methodList = GetActionNameList(g102Entity.G1028.Trim());  //获取所有的处理任务 
                    foreach (var strAction in methodList)
                    {
                        GActionBusiness.ExcuteG102Action(strAction, strOperatorName, reportHeaderEntity, g102Entity);
                    }

                }
            }
        }


        /// <summary>
        /// 查询 g130(report  category)中的处理任务,查看g102中的任务列表,并产生消息到 G100,或者直接执行
        /// </summary>
        /// <param name="reportHeaderEntity"></param>
        public static void GenerateReportFlowTask(ReportHeaderEntity reportHeaderEntity,string strOperatorName)
        {
            var actionList = reportHeaderEntity.ReportCategory.G1307.SeperateString('|');
            foreach (var strActionName in actionList)
            {
                var g102Entity = ActionBusiness.GetG102EntityByActionName(strActionName);
                if (g102Entity != null)
                {
                    var methodList = GetActionNameList(g102Entity.G1028.Trim());  //获取所有的处理任务 
                    foreach (var strAction in methodList)
                    {
                        GActionBusiness.ExcuteG102Action(strAction, strOperatorName, reportHeaderEntity, g102Entity);
                    }

                }
            }
        }


        /// <summary>
        /// 判断输入值
        ///a.表單操作=1XXX :鼠標點擊訊息條時執行顯示單號頁面提供操作										
        ///b.表單審核模式1=2XXX :鼠標點擊訊息條時執行顯示單號頁面並提供表單操作.										
        ///c.表單審核模式2=4XXX :保留未使用.										
        ///d.訊息通知=8XXX :只提供通知內容不處理其他表單功能.										
        ///e.短訊通知=X1XX :取得(通訊載體(G102-15))及通知內容(G102-4))發至手機										
        ///f.郵箱通知=X2XX :得電子郵箱(通訊載體(G102-15))及通知內容(G102-4)發至郵箱										
        ///g.外部通訊=X4XX :執行外部PC間TCP/IP訊息傳遞,取得IP(通訊載體(G102-15))及外部通訊內容(G102-4)										
        ///h.內部通訊=X8XX :執行本機API間TCP/IP訊息傳遞,取得IP(通訊載體(G102-15))及內部通訊內容(G102-4))										
        ///i.信息交换=XX1X :執行B115功能模組通訊協議.										
        ///j.時間記錄=XX2X :將回應時間及通知對象(操作人員)記錄至G400相對應的顯示單號欄位										
        ///k.看板顯示=XX4X :執行G102-2 - G102-6 之內容顯示到被通知對象管理看板										
        ///l.計算操作=XX8X :執行事件計算器(G107)內計算器代碼. 啟動事件計算器(G107)執行SQL內觸發器計算組數內計算器公式計算條件. 	
        /// </summary>
        /// <param name="strActionNames"></param>
        /// <returns></returns>
        public static List<string> GetActionNameList(string strActionNames)
        {
            var listReturn = new List<string>();

            if (strActionNames.Length == 4)
            {
                int intIndex = 1;  //左数第一个位置 开始计数
                var chrList = strActionNames.ToCharArray();
                foreach (char chr in chrList)
                {
                    #region
                    if (intIndex == 1)
                    {
                        if (chr == '1')   // 1
                        {
                            listReturn.Add(PublicConst.G102Method.Action1);
                        }

                        if (chr == '2')  // 2
                        {
                            listReturn.Add(PublicConst.G102Method.Action2);
                        }

                        if (chr == '3') //1+2
                        {
                            listReturn.Add(PublicConst.G102Method.Action1);
                            listReturn.Add(PublicConst.G102Method.Action2);
                        }

                        if (chr == '4')  //4
                        {
                            listReturn.Add(PublicConst.G102Method.Action3);
                        }

                        if (chr == '5')//4+1
                        {
                            listReturn.Add(PublicConst.G102Method.Action1);
                            listReturn.Add(PublicConst.G102Method.Action3);
                        }
                        if (chr == '6')//4+2
                        {
                            listReturn.Add(PublicConst.G102Method.Action2);
                            listReturn.Add(PublicConst.G102Method.Action3);
                        }

                        if (chr == '7')//4+2+1
                        {
                            listReturn.Add(PublicConst.G102Method.Action1);
                            listReturn.Add(PublicConst.G102Method.Action2);
                            listReturn.Add(PublicConst.G102Method.Action3);
                        }

                        if (chr == '8')//8
                        {
                            listReturn.Add(PublicConst.G102Method.Action4);
                        }

                        if (chr == '9')//8+1
                        {
                            listReturn.Add(PublicConst.G102Method.Action1);
                            listReturn.Add(PublicConst.G102Method.Action4);
                        }
                        if (chr == 'A' || chr == 'a')  //10  8+2
                        {
                            listReturn.Add(PublicConst.G102Method.Action2);
                            listReturn.Add(PublicConst.G102Method.Action4);
                        }
                        if (chr == 'B' || chr == 'b')   //11   8+2+1
                        {
                            listReturn.Add(PublicConst.G102Method.Action1);
                            listReturn.Add(PublicConst.G102Method.Action2);
                            listReturn.Add(PublicConst.G102Method.Action4);
                        }
                        if (chr == 'C' || chr == 'c')   //12   8+4
                        {
                            listReturn.Add(PublicConst.G102Method.Action3);
                            listReturn.Add(PublicConst.G102Method.Action4);
                        }

                        if (chr == 'D' || chr == 'd')   //13   8+4 +1
                        {
                            listReturn.Add(PublicConst.G102Method.Action1);
                            listReturn.Add(PublicConst.G102Method.Action3);
                            listReturn.Add(PublicConst.G102Method.Action4);
                        }
                        if (chr == 'E' || chr == 'e')   //13   8+4 +2
                        {
                            listReturn.Add(PublicConst.G102Method.Action2);
                            listReturn.Add(PublicConst.G102Method.Action3);
                            listReturn.Add(PublicConst.G102Method.Action4);
                        }
                        if (chr == 'F' || chr == 'f')   //13   8+4+2 +1
                        {
                            listReturn.Add(PublicConst.G102Method.Action1);
                            listReturn.Add(PublicConst.G102Method.Action2);
                            listReturn.Add(PublicConst.G102Method.Action3);
                            listReturn.Add(PublicConst.G102Method.Action1);
                        }
                    }
                    #endregion
                    #region
                    if (intIndex == 2)
                    {
                        if (chr == '1')   // 1
                        {
                            listReturn.Add(PublicConst.G102Method.Action5);
                        }

                        if (chr == '2')  // 2
                        {
                            listReturn.Add(PublicConst.G102Method.Action6);
                        }

                        if (chr == '3') //1+2
                        {
                            listReturn.Add(PublicConst.G102Method.Action5);
                            listReturn.Add(PublicConst.G102Method.Action6);
                        }

                        if (chr == '4')  //4
                        {
                            listReturn.Add(PublicConst.G102Method.Action7);
                        }

                        if (chr == '5')//4+1
                        {
                            listReturn.Add(PublicConst.G102Method.Action5);
                            listReturn.Add(PublicConst.G102Method.Action7);
                        }
                        if (chr == '6')//4+2
                        {
                            listReturn.Add(PublicConst.G102Method.Action6);
                            listReturn.Add(PublicConst.G102Method.Action7);
                        }

                        if (chr == '7')//4+2+1
                        {
                            listReturn.Add(PublicConst.G102Method.Action5);
                            listReturn.Add(PublicConst.G102Method.Action6);
                            listReturn.Add(PublicConst.G102Method.Action7);
                        }

                        if (chr == '8')//8
                        {
                            listReturn.Add(PublicConst.G102Method.Action8);
                        }

                        if (chr == '9')//8+1
                        {
                            listReturn.Add(PublicConst.G102Method.Action5);
                            listReturn.Add(PublicConst.G102Method.Action8);
                        }
                        if (chr == 'A' || chr == 'a')  //10  8+2
                        {
                            listReturn.Add(PublicConst.G102Method.Action6);
                            listReturn.Add(PublicConst.G102Method.Action8);
                        }
                        if (chr == 'B' || chr == 'b')   //11   8+2+1
                        {
                            listReturn.Add(PublicConst.G102Method.Action5);
                            listReturn.Add(PublicConst.G102Method.Action6);
                            listReturn.Add(PublicConst.G102Method.Action8);
                        }
                        if (chr == 'C' || chr == 'c')   //12   8+4
                        {
                            listReturn.Add(PublicConst.G102Method.Action7);
                            listReturn.Add(PublicConst.G102Method.Action8);
                        }

                        if (chr == 'D' || chr == 'd')   //13   8+4 +1
                        {
                            listReturn.Add(PublicConst.G102Method.Action5);
                            listReturn.Add(PublicConst.G102Method.Action7);
                            listReturn.Add(PublicConst.G102Method.Action8);
                        }
                        if (chr == 'E' || chr == 'e')   //13   8+4 +2
                        {
                            listReturn.Add(PublicConst.G102Method.Action6);
                            listReturn.Add(PublicConst.G102Method.Action7);
                            listReturn.Add(PublicConst.G102Method.Action8);
                        }
                        if (chr == 'F' || chr == 'f')   //13   8+4+2 +1
                        {
                            listReturn.Add(PublicConst.G102Method.Action5);
                            listReturn.Add(PublicConst.G102Method.Action6);
                            listReturn.Add(PublicConst.G102Method.Action7);
                            listReturn.Add(PublicConst.G102Method.Action5);
                        }
                    }
                    #endregion
                    #region
                    if (intIndex == 3)
                    {
                        if (chr == '1')   // 1
                        {
                            listReturn.Add(PublicConst.G102Method.Action9);
                        }

                        if (chr == '2')  // 2
                        {
                            listReturn.Add(PublicConst.G102Method.Action10);
                        }

                        if (chr == '3') //1+2
                        {
                            listReturn.Add(PublicConst.G102Method.Action9);
                            listReturn.Add(PublicConst.G102Method.Action10);
                        }

                        if (chr == '4')  //4
                        {
                            listReturn.Add(PublicConst.G102Method.Action11);
                        }

                        if (chr == '5')//4+1
                        {
                            listReturn.Add(PublicConst.G102Method.Action9);
                            listReturn.Add(PublicConst.G102Method.Action11);
                        }
                        if (chr == '6')//4+2
                        {
                            listReturn.Add(PublicConst.G102Method.Action10);
                            listReturn.Add(PublicConst.G102Method.Action11);
                        }

                        if (chr == '7')//4+2+1
                        {
                            listReturn.Add(PublicConst.G102Method.Action9);
                            listReturn.Add(PublicConst.G102Method.Action10);
                            listReturn.Add(PublicConst.G102Method.Action11);
                        }

                        if (chr == '8')//8
                        {
                            listReturn.Add(PublicConst.G102Method.Action12);
                        }

                        if (chr == '9')//8+1
                        {
                            listReturn.Add(PublicConst.G102Method.Action9);
                            listReturn.Add(PublicConst.G102Method.Action12);
                        }
                        if (chr == 'A' || chr == 'a')  //10  8+2
                        {
                            listReturn.Add(PublicConst.G102Method.Action10);
                            listReturn.Add(PublicConst.G102Method.Action12);
                        }
                        if (chr == 'B' || chr == 'b')   //11   8+2+1
                        {
                            listReturn.Add(PublicConst.G102Method.Action9);
                            listReturn.Add(PublicConst.G102Method.Action10);
                            listReturn.Add(PublicConst.G102Method.Action12);
                        }
                        if (chr == 'C' || chr == 'c')   //12   8+4
                        {
                            listReturn.Add(PublicConst.G102Method.Action11);
                            listReturn.Add(PublicConst.G102Method.Action12);
                        }

                        if (chr == 'D' || chr == 'd')   //13   8+4 +1
                        {
                            listReturn.Add(PublicConst.G102Method.Action9);
                            listReturn.Add(PublicConst.G102Method.Action11);
                            listReturn.Add(PublicConst.G102Method.Action12);
                        }
                        if (chr == 'E' || chr == 'e')   //13   8+4 +2
                        {
                            listReturn.Add(PublicConst.G102Method.Action10);
                            listReturn.Add(PublicConst.G102Method.Action11);
                            listReturn.Add(PublicConst.G102Method.Action12);
                        }
                        if (chr == 'F' || chr == 'f')   //13   8+4+2 +1
                        {
                            listReturn.Add(PublicConst.G102Method.Action9);
                            listReturn.Add(PublicConst.G102Method.Action10);
                            listReturn.Add(PublicConst.G102Method.Action11);
                            listReturn.Add(PublicConst.G102Method.Action9);
                        }
                    }
                    #endregion
                    #region

                    if (intIndex == 4)
                    {
                        if (chr == '1')   // 1
                        {
                            listReturn.Add(PublicConst.G102Method.Action13);
                        }

                    }
                    #endregion

                    intIndex++;
                }



            }

            return listReturn;
        }
    }
}
